﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.BanBuon
{
    public partial class frmChietKhauThuongMai : Form
    {
        public static bool kiemtra = false;
        bool checkClose = false;
        bool check = false;
        Entities.HDBanBuon temp;
        public static Entities.HDBanBuon hdbanbuon;
        double chietkhau;

        public frmChietKhauThuongMai()
        {
            InitializeComponent();
        }
        public frmChietKhauThuongMai(string hanhDong, Entities.HDBanBuon hdbanbuon, bool kt, double tongtienhang)
        {
            InitializeComponent();
            try
            {

                this.chietkhau = double.Parse(hdbanbuon.ChietKhau);
                btnThanhToan.Enabled = kt;
                this.temp = hdbanbuon;
                lbTongTien.Text = Common.Utilities.FormatMoney(tongtienhang.ToString());
                txtChietKhauThuongMai.Text = hdbanbuon.PhanTramChietKhauThuongmai.ToString();
                if (hanhDong != Common.Constants.Insert)
                {
                    txtChietKhauThuongMai.ReadOnly = true;
                }
                txtChietKhauThuongMai.Focus();
            }
            catch
            {
            }
        }

        void ThanhToan()
        {
            try
            {
                if (!btnThanhToan.Enabled)
                {
                    return;
                }
                int chietkhautm = 0;
                if (txtChietKhauThuongMai.Text != "")
                    chietkhautm = int.Parse(txtChietKhauThuongMai.Text);
                temp.PhanTramChietKhauThuongmai = chietkhautm;
                temp.ChietKhau = (double.Parse(lbTienChietKhau.Text)).ToString();
                hdbanbuon = temp;
                if (MessageBox.Show("Bạn muốn thanh toán luôn không?", Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    checkClose = true;
                    this.Close();
                }
                else
                {
                    kiemtra = true;
                    checkClose = true;
                    this.Close();
                }
            }
            catch
            {
            }
        }

        void Thoat()
        {
            this.Close();
        }

        private void frmChietKhauThuongMai_Load(object sender, EventArgs e)
        {
            this.FormClosing += new FormClosingEventHandler(frmChietKhauThuongMai_FormClosing);
            btnThanhToan.Click +=new EventHandler(btnThanhToan_Click);
            btnThoat.Click +=new EventHandler(btnThoat_Click);
            txtChietKhauThuongMai.TextChanged += new EventHandler(txtChietKhauThuongMai_TextChanged);
        }

        void txtChietKhauThuongMai_TextChanged(object sender, EventArgs e)
        {
            try
            {
                double i = double.Parse(txtChietKhauThuongMai.Text);
                if (i < 0 || i > 100)
                {
                    txtChietKhauThuongMai.Text = "";
                    lbTienChietKhau.Text = "0";
                    return;
                }

                lbTienChietKhau.Text = Common.Utilities.FormatMoney(((double.Parse(lbTongTien.Text) * i) / 100).ToString());
            }
            catch
            {
                txtChietKhauThuongMai.Text = "";
                lbTienChietKhau.Text = "0";
            }
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void btnThanhToan_Click(object sender, EventArgs e)
        {
            ThanhToan();
        }

        void frmChietKhauThuongMai_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    kiemtra = true;
                    e.Cancel = checkClose;
                }
                else
                    e.Cancel = !checkClose;
        }
    }
}
#endregion